﻿using ActiproSoftware.Products.Ribbon;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using CCTool.Scripts.Manager;
using CCTool.Scripts.ToolManagers;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Library;
using CCTool.Scripts.ToolManagers.Managers;
using Microsoft.Office.Core;
using NPOI.OpenXmlFormats.Dml.Chart;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CCTool.Scripts.UI
{
    /// <summary>
    /// Interaction logic for ZQHD.xaml
    /// </summary>
    public partial class ZQHD : ArcGIS.Desktop.Framework.Controls.ProWindow
    {
        // 工具设置标签
        readonly string toolSet = "ZQHD";

        public ZQHD()
        {
            InitializeComponent();
            // 初始化combox

            // 初始化参数选项
            textGDBPath.Text = BaseTool.ReadValueFromReg(toolSet, "gdbPath", "");
            textOutputPath.Text = BaseTool.ReadValueFromReg(toolSet, "outputPath", "");

            txt_xzqdm.Text = BaseTool.ReadValueFromReg(toolSet, "xzqdm", "");
            txt_xzqmc.Text = BaseTool.ReadValueFromReg(toolSet, "xzqmc", "");
        }

        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "镇区划定工具";


        private void openGDBButton_Click(object sender, RoutedEventArgs e)
        {
            textGDBPath.Text = UITool.OpenDialogGDB();
        }

        private void openOutputButton_Click(object sender, RoutedEventArgs e)
        {
            textOutputPath.Text = UITool.OpenDialogFolder();
        }

        private async void btn_go_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ReportData rd = new ReportData();

                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);


                pw.AddMessageStart("获取参数");

                // 获取参数
                string gdbPath = textGDBPath.Text;
                string outputPath = textOutputPath.Text;

                string xzqdm = txt_xzqdm.Text;
                string xzqmc = txt_xzqmc.Text;


                // 写入本地
                BaseTool.WriteValueToReg(toolSet, "gdbPath", gdbPath);
                BaseTool.WriteValueToReg(toolSet, "outputPath", outputPath);

                BaseTool.WriteValueToReg(toolSet, "xzqdm", txt_xzqdm.Text);
                BaseTool.WriteValueToReg(toolSet, "xzqmc", txt_xzqmc.Text);


                string dltb = $@"{gdbPath}\地类图斑";
                string czcyd = $@"{gdbPath}\城镇村用地";

                string outGDB = $@"{outputPath}\{xzqdm}{xzqmc}.gdb";

                string zqcsfw = $@"{outGDB}\{xzqmc}zqcsfw";
                string zqstdy = $@"{outGDB}\{xzqmc}zqstdy";


                rd.DLTB = dltb;
                rd.ZQSTDY = zqstdy;

                Close();

                await QueuedTask.Run(() =>
                {

                    // 创建GDB
                    string outGDB = $@"{outputPath}\{xzqdm}{xzqmc}.gdb";

                    if (Directory.Exists(outGDB))
                    {
                        Arcpy.Delect(outGDB);
                    }
                    Arcpy.CreateFileGDB(outputPath, $"{xzqdm}{xzqmc}");


                    pw.AddMessageMiddle(10, "初始范围");

                    // 选择出初始范围
                    Arcpy.Select(dltb, zqcsfw, $"CZCSXM IN ('202', '202A')");
                    // 复制到实体地域范围
                    Arcpy.CopyFeatures(zqcsfw, zqstdy);


                    pw.AddMessageMiddle(10, "第一次迭代");
                    // 缓冲迭代计算
                    BufferAndCal(rd, true);

                    pw.AddMessageMiddle(10, "第二次迭代");
                    BufferAndCal(rd, false);

                    pw.AddMessageMiddle(10, "第三次迭代");
                    BufferAndCal(rd, false);
                });

                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }

        // 缓冲迭代计算
        private void BufferAndCal(ReportData rd, bool isFirst = false)
        {
            string zqstdy = rd.ZQSTDY;
            string dltb = rd.DLTB;
            List<string> sourceIDs = rd.SourceIDs;


            string defGDB = Project.Current.DefaultGeodatabasePath;
            string buffer = $@"{defGDB}\buffer";

            string selectST = $@"{defGDB}\selectST";

            // 如果不是第一次，就排除掉一些地类图斑
            if (!isFirst)
            {
                Arcpy.Select(zqstdy, selectST, "DLMC NOT IN ('铁路用地', '轨道交通用地', '公路用地', '城镇村道路用地', '管道运输用地', '农村道路','沟渠')");
                zqstdy = selectST;
            }

            // 缓冲
            Arcpy.Buffer(zqstdy, buffer, "100 Meters");

            // 空间连接
            string spatialJoinPart = $@"{defGDB}\spatialJoinPart";
            string spatialJoinPart2 = $@"{defGDB}\spatialJoinPart2";
            Arcpy.SpatialJoin(dltb, buffer, spatialJoinPart);
            Arcpy.Select(spatialJoinPart, spatialJoinPart2, "Join_Count = 1");

            // 擦除
            string erase2 = $@"{defGDB}\erase2";
            Arcpy.Erase(spatialJoinPart2, rd.ZQSTDY, erase2);

            // 选择
            string selectST2 = $@"{defGDB}\selectST2";
            Arcpy.Select(erase2, selectST2, "DLBM IN ('05H1', '0508', '0601', '0701', '0702','08H1','08H2','08H1','0809','0810','09','1001','1002','1003','1004','1005','1006','1007','1008','08H1','1009','1107','1109')");


            Arcpy.Append(selectST2, rd.ZQSTDY);
        }

    }


    public class ReportData
    {
        public List<string> SourceIDs { get; set; }
        public string DLTB { get; set; }
        public string ZQSTDY { get; set; }
    }
}
